package com.craftcomputing.client.Listeners;

import com.craftcomputing.client.MainPage;
import com.craftcomputing.client.Entry.EntryPanel;
import com.craftcomputing.client.SearchResult.SearchResult;
import com.google.gwt.json.client.*;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.*;
import com.google.gwt.core.client.*;

public class SearchResultListeners {
	static public class JSONEntryFetchTextHandler implements ResponseTextHandler {		
		private EntryPanel entry;
		public JSONEntryFetchTextHandler(EntryPanel entry){
			this.entry = entry;
		}
		public void onCompletion(String responseText) {
			try {
				JSONValue jsonValue = JSONParser.parse(responseText);
				if (jsonValue != null){
					JSONArray jsonArray = jsonValue.isArray();
					if (jsonArray != null) {
						entry.updateEntry(jsonArray.get(0));
					}					
				}
			} catch (JSONException e) {
			}
		}
	}

	static public class JSONSearchAllResponseTextHandler implements ResponseTextHandler {
		private class myComp extends Composite{			
			public myComp(SearchResult[] searchResults, JSONArray jsonArray){
				VerticalPanel panel = new VerticalPanel();
				initWidget(panel);
				for(int i = 0; i < searchResult.length; i++){
					String count = jsonArray.get(2*i).isObject().get("count").toString().replaceAll("\"", "");
					searchResult[i].setCount(count);
					searchResult[i].setLabels((JSONArray)jsonArray.get(2*i+1), searchText);
					panel.add(searchResult[i]);
				}
				MainPage.setMainPanel(this);
			}
		}
		private JSONValue jsonValue;
		private SearchResult[] searchResult;
		private String searchText;
		public JSONSearchAllResponseTextHandler(SearchResult[] searchResult, String searchText){
			this.searchResult = searchResult;
			this.searchText = searchText;
		}
		public void onCompletion(String responseText) {
			try {
				jsonValue = JSONParser.parse(responseText);
				if (jsonValue != null){
					JSONArray jsonArray = jsonValue.isArray();
					if (jsonArray != null) {
						myComp comp = new myComp(searchResult, jsonArray);
						MainPage.setMainPanel(comp);
					}					
				}
			} catch (JSONException e) {
			}
		}
	}
	static public class JSONSearchResponseTextHandler implements ResponseTextHandler {
		private JSONValue jsonValue;
		private SearchResult searchResult;
		private String searchText;
		public JSONSearchResponseTextHandler(SearchResult searchResult, String searchText){
			this.searchResult = searchResult;
			this.searchText = searchText;
		}
		public void onCompletion(String responseText) {
			try {
				jsonValue = JSONParser.parse(responseText);
				if (jsonValue != null){
					JSONArray jsonArray = jsonValue.isArray();
					if (jsonArray != null) {
						searchResult.setLabels(jsonArray, searchText);
						MainPage.setMainPanel(this.searchResult);
					}					
				}
			} catch (JSONException e) {
			}
		}
	}
	static public class JSONLinkedEntryResponseTextHandler implements ResponseTextHandler {
		private JSONValue jsonValue;
		private SearchResult searchResult;
		public JSONLinkedEntryResponseTextHandler(SearchResult searchResult){
			this.searchResult = searchResult;
		}
		public void onCompletion(String responseText) {
			try {
				jsonValue = JSONParser.parse(responseText);
				if (jsonValue != null){
					JSONArray jsonArray = jsonValue.isArray();
					if (jsonArray != null) {
						searchResult.setLabels(jsonArray, "");
					}					
				}
			} catch (JSONException e) {
			}
		}
	}
	static public class JSONLinkedEntryLabelResponseTextHandler implements ResponseTextHandler {
		private JSONValue jsonValue;
		private SearchResult searchResult;
		public JSONLinkedEntryLabelResponseTextHandler(SearchResult searchResult){
			this.searchResult = searchResult;
		}
		public void onCompletion(String responseText) {
			try {
				jsonValue = JSONParser.parse(responseText);
				if (jsonValue != null){
					JSONArray jsonArray = jsonValue.isArray();
					if (jsonArray != null) {
						if(jsonArray.get(0).isObject() != null)
						searchResult.addLabel(jsonArray.get(0));
					}					
				}
			} catch (JSONException e) {
			}
		}
	}
	static public class JSONCountResponseTextHandler implements ResponseTextHandler {
		private SearchResult searchResult;
		public JSONCountResponseTextHandler(SearchResult searchResult){
			this.searchResult = searchResult;
		}
		public void onCompletion(String responseText) {
			try {
				JSONValue jsonValue = JSONParser.parse(responseText);
				if (jsonValue != null){
					String count = jsonValue.isObject().get("count").toString().replaceAll("\"", "");
					searchResult.setCount(count);
				}
			} catch (JSONException e) {
			}
		}
	}

	static public class SearchResultKeyboardListener extends KeyboardListenerAdapter {
		private SearchResult searchResult;
		private TextBox tb;
		public SearchResultKeyboardListener(SearchResult searchResult, TextBox tb){
			this.searchResult = searchResult;			
			this.tb = tb;
		}
        public void onKeyPress(Widget sender, char keyCode, int modifiers) {
	          if (keyCode == (char) KEY_ENTER){
	        	  searchResult.doSearch(tb.getText(), true);
	          }
        }		
	}
	static public class SearchResultClickListener implements ClickListener {
		private SearchResult searchResult;
		private TextBox tb;
		public SearchResultClickListener(SearchResult searchResult, TextBox tb){
			this.searchResult = searchResult;			
			this.tb = tb;			
		    tb.addKeyboardListener(
		    		new SearchResultKeyboardListener(searchResult, tb));
		}
		public void onClick(Widget sender) {
      	  searchResult.doSearch(tb.getText(), true);
		}
	}
	static public class SearchResultMouseListener implements MouseListener{
		private Label label;
		private JSONValue json;
		private SearchResult searchResult;
		public SearchResultMouseListener(SearchResult searchResult, Label label, JSONValue json){
			this.searchResult = searchResult;			
			this.label = label;
			this.json = json;
		}
		public void onMouseDown(final Widget sender, final int x, final int y){
			EntryPanel entry = searchResult.makeEntryPanel();
			entry.updateEntry(json);
			MainPage.setMainPanel(entry); 
		}
		public void onMouseEnter(final Widget sender) {
			label.setStyleName("gwt-Result-Over");		   
		}
		public void onMouseLeave(final Widget sender) {
			label.setStyleName("gwt-Result");		   
		}
		public void onMouseMove(final Widget sender, final int x, final int y) {}
		public void onMouseUp(final Widget sender, final int x, final int y) {}
	}
	public static class NextClickListener implements ClickListener{
		private SearchResult searchResult;
		public NextClickListener(SearchResult searchResult){
			this.searchResult = searchResult;
		}
		public void onClick(Widget sender) {
			searchResult.setOffset(searchResult.getOffset() + 10);
			searchResult.doSearch(searchResult.getTextBox().getText(), false);
		}			
	}
	public static class LastClickListener implements ClickListener{
		private SearchResult searchResult;
		public LastClickListener(SearchResult searchResult){
			this.searchResult = searchResult;
		}
		public void onClick(Widget sender) {
			searchResult.setOffset(searchResult.getOffset() - 10);
			searchResult.doSearch(searchResult.getTextBox().getText(), false);
		}			
	}
}
